{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a3aa707e",
   "metadata": {},
   "source": [
    "# 1.numpy的核心array对象以及创建array的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ef1d7ba",
   "metadata": {},
   "source": [
    "## array是核心数据结构，就是数组。支持大量操作和函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e08e3489",
   "metadata": {},
   "source": [
    "### 最一般方法：通过array来构造"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b8c3cbaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "one = np.array([1,2,3,4])\n",
    "one"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd65092a",
   "metadata": {},
   "source": [
    "#### 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3a2d40bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two = np.array(\n",
    "    [[1,2,3,4],\n",
    "    [4,5,6,7]])\n",
    "two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "235682a1",
   "metadata": {},
   "source": [
    "#### 二维数组。注意：有3个括号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61b546a9",
   "metadata": {},
   "source": [
    "# 2.探索数组array的属性：4个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "396df558",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1e599d0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two.shape # 行和列。有两行四列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d2113bdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one.ndim # ndim用来查看维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7b54c1c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7ef8de6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one.size # size用来查看元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e044da63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d2921308",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one.dtype # dtype用来查看数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c2865c61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df61b1ad",
   "metadata": {},
   "source": [
    "# 3. 创建array的便捷函数：特殊数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2811ec5",
   "metadata": {},
   "source": [
    "### 等差序列 np.linspace,np.arange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d2c8d10a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5., 6.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,6,6) # (起始值，终止值（包含），样本个数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd3f8b20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1. , 3.5, 6. ])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,6,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63935cc9",
   "metadata": {},
   "source": [
    "### arange函数用于创建等差数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0dc2beff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10) # 从0开始，<10的数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be4865b1",
   "metadata": {},
   "source": [
    "#### 起始值，终止（不包含），步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cc8036ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,1) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9adbf405",
   "metadata": {},
   "source": [
    "### ones函数。创建全是1的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0de8ded8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(10) # 全是1，有10个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2cfb2343",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3)) # 创建2行3列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0867f70",
   "metadata": {},
   "source": [
    "### 特殊矩阵。zeros（数值全是0），eye，full（填充指定值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8fb883dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([666, 666, 666, 666, 666, 666, 666, 666, 666, 666])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full(10,666) # 10个666"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "50cd9653",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[888, 888, 888, 888],\n",
       "       [888, 888, 888, 888],\n",
       "       [888, 888, 888, 888]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((3,4),888)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c6112e4",
   "metadata": {},
   "source": [
    "### 随机矩阵。np.random"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6df7b96",
   "metadata": {},
   "source": [
    "### 最常用的随机生成函数：\n",
    "- rand 0-1均匀分布的随机数组\n",
    "- randn 标准正太的随机数组\n",
    "- randint 随机整数组\n",
    "- choice 随机列表抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "35f598e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.27711095, 0.10065453, 0.41710895, 0.64935036, 0.46731648])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(5) # 0-1的均匀分布的5个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "450f7976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.00168471, 0.80226972, 0.79156005])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(3) # 个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c45af52d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.64010637, 0.10486479, 0.8546472 , 0.08251186],\n",
       "       [0.63851958, 0.37604689, 0.98205178, 0.23298236],\n",
       "       [0.53031432, 0.09295852, 0.19707733, 0.42084341],\n",
       "       [0.11871174, 0.21860251, 0.84011969, 0.90195742]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(4,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070a3ddd",
   "metadata": {},
   "source": [
    "### 注意这里传入的不是元组，每个维度大小分开输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0c8ea1ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.8384148 , 0.94491041],\n",
       "       [0.07674889, 0.61783795],\n",
       "       [0.18577379, 0.06629045],\n",
       "       [0.50102323, 0.45387607]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(4,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7815d1cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.34424793, 0.67563865],\n",
       "       [0.51954271, 0.71982849]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(2,2) # 组数/维度，每组的个数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b392d48",
   "metadata": {},
   "source": [
    "### 库函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5bd6d2aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.69378958, 14.99500225, 14.12329335])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(5, 15, 3) # 在区间5-15取3个数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70e31203",
   "metadata": {},
   "source": [
    "# reshape 更改与重塑维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7062a369",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c2fd69fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(10).reshape(2,5)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "87b5aaae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 5)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9bcd7d2",
   "metadata": {},
   "source": [
    "# 可以直接做加减乘除法运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2454b482",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e93d89de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A+1 # 直接做加减法。在原来数组的数值上+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "8f43bff5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-3, -2, -1,  0,  1],\n",
       "       [ 2,  3,  4,  5,  6]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "99457fc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2,  4,  6,  8],\n",
       "       [10, 12, 14, 16, 18]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "36a81b5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.        , 0.33333333, 0.66666667, 1.        , 1.33333333],\n",
       "       [1.66666667, 2.        , 2.33333333, 2.66666667, 3.        ]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ea3b187a",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.random.randn(2,5) # 随机生成2排的5个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "3aacd07d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.65171851,  0.33294347,  2.04459166,  0.5894747 ,  0.51331112],\n",
       "       [-0.13240441, -1.12436648,  0.10372321, -1.19445541,  0.18464134]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f1a8b6bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.65171851, 1.33294347, 4.04459166, 3.5894747 , 4.51331112],\n",
       "       [4.86759559, 4.87563352, 7.10372321, 6.80554459, 9.18464134]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A+B "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1d0d812f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 5)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f4a63a3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 5)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b2f842d",
   "metadata": {},
   "source": [
    "### 因为A与B的形状相同，所以可以计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "040a716b",
   "metadata": {},
   "source": [
    "# 总结：\n",
    "- numpy的核心array对象\n",
    "- array本身的属性\n",
    "- 创建方法及函数\n",
    "- random随机模块\n",
    "- reshape重塑维度\n",
    "- 相同形状的数组可以直接计算"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
